package com.sgcc.ln.pmos.e_ln_mall.presenter;


import android.text.TextUtils;

import com.sgcc.ln.pmos.common.base.BasePresenter;
import com.sgcc.ln.pmos.common.bean.BaseResponse;
import com.sgcc.ln.pmos.common.http.HttpInterConst;
import com.sgcc.ln.pmos.common.http.HttpRequestUtils;
import com.sgcc.ln.pmos.common.http.MvpNetCallback;
import com.sgcc.ln.pmos.common.utils.DateUtils;
import com.sgcc.ln.pmos.e_ln_mall.bean.MonthEnergyBean;
import com.sgcc.ln.pmos.e_ln_mall.bean.StoreHomeBean;
import com.sgcc.ln.pmos.e_ln_mall.bean.StoreYearBean;
import com.sgcc.ln.pmos.e_ln_mall.contract.StoreHomePageContract;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class StoreHomePagePresenter extends BasePresenter<StoreHomePageContract.View> implements StoreHomePageContract.Presenter {


    @Override
    public void getStoreDetails(String membersSdId) {
        Map<String, Object> params = new HashMap<>();
        params.put("membersSdId", membersSdId);
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.MALL_QUERYDATAINFO, new MvpNetCallback<StoreHomeBean>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                if (!isViewAttached()) {
                    return;
                }
                // 失败回调 判断界面是否销毁
                getView().queryFailed();
            }

            @Override
            public void onResponse(StoreHomeBean response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().querySuccess(response);
            }
        });
    }


    /**
     * 加入收藏
     *
     * @param membersUserName
     * @param membersSdId
     * @param membersSdName
     */
    @Override
    public void addUserPackageCollection(String membersUserName, String membersSdId, String membersSdName) {
        Map<String, Object> params = new HashMap<>();
        params.put("membersUserName", membersUserName);
        params.put("membersSdId", membersSdId);
        params.put("membersSdName", membersSdName);
        params.put("ptype", "0");
        params.put("ustatus", "01");
        params.put("remark", "");
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.MALL_PACKAGE_COLLECTION, new MvpNetCallback<BaseResponse>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
                getView().addCollectionFailed();
            }

            @Override
            public void onResponse(BaseResponse response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().addCollectionSuccess(response);
            }
        });
    }

    /**
     * 取消收藏商品/店铺
     */
    @Override
    public void deletePackageById(String membersSdId) {
        Map<String, Object> params = new HashMap<>();
        params.put("ptype", "0");
        params.put("updatePersonId", "");
        params.put("membersSdId", membersSdId);
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.MALL_PACKAGE_DELE_COLLECTION, new MvpNetCallback<BaseResponse>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
                getView().delCollectionFailed();
            }

            @Override
            public void onResponse(BaseResponse response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().delCollectionSuccess(response);
            }
        });
    }


    /**
     * 获取次月已售和可售电量
     */
    @Override
    public void queryMonthEnergy(String membersSdId,String ym) {
        Map<String, Object> params = new HashMap<>();
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.MONTH,1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        if(TextUtils.isEmpty(ym)){
            params.put("ym", sdf.format(calendar.getTime()));
        }else{
            params.put("ym",  ym.replace("-",""));
        }

        params.put("membersSdId", membersSdId);
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.MALL_MONTHENERGY, new MvpNetCallback<MonthEnergyBean>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
            }

            @Override
            public void onResponse(MonthEnergyBean response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().queryMonthEnergySuccess(response);
            }
        });
    }
    @Override
    public void queryYear(String membersSdId,String year) {
        Map<String, Object> params = new HashMap<>();
        params.put("membersSdId", membersSdId);
        params.put("year", year);
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.MALL_QUERYYAERENERGY, new MvpNetCallback<StoreYearBean>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                if (!isViewAttached()) {
                    return;
                }
                // 失败回调 判断界面是否销毁
                getView().queryFailed();
            }

            @Override
            public void onResponse(StoreYearBean response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().queryYearSuccess(response);
            }
        });
    }
}
